//
//  GETWeatherInfoTool.m
//  天气实验
//
//  Created by dllo on 16/3/8.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "GETWeatherInfoTool.h"

@implementation GETWeatherInfoTool

+(void)getWeahterInfoWithRequest:(NSString *)httpUrl andHttpArg:(NSString *)HttpArg Block:(MYBLOCK)block error:(AppToolsErrorBlock)errorBlock{
    
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"6c3883518a88ccbafd699fd63e5f6439" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
//                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
//                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//                                   NSLog(@"HttpResponseCode:%ld", responseCode);
//                                   NSLog(@"HttpResponseBody %@",responseString);
                         
                                   id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                                   if (error == nil) {
                                       //传给block
                                       block(result);
                                   } else {
                                       
                                       //返回错误信息
                                       errorBlock(error);
                                   }
                               }
                           }];
    
    
    NSLog(@"wo shi li si!");
    
    
}

@end
